package com.sdhs.paas.gateway.util;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Component;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriTemplate;

@Component
public class RestTemplateUtil
{
  public static String parseUrl(String url, HashMap uriVariables)
  {
    return parseUrl(url, uriVariables, null);
  }

  public static String parseUrl(String url, HashMap uriVariables, HashMap<String, String> getParameters)
  {
    URI expanded = new UriTemplate(url).expand(uriVariables);
    try {
      url = URLDecoder.decode(expanded.toString(), "UTF-8");
    } catch (UnsupportedEncodingException e) {
//      PaasLogger.info("服务器异常：{}", new Object[] { e.getMessage() });
    }

    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
    if (getParameters != null) {
      for (Map.Entry entry : getParameters.entrySet()) {
        builder.queryParam((String)entry.getKey(), new Object[] { entry.getValue() });
      }
    }
    String uri = builder.build().toString();

    return uri;
  }

  public static Object get(String url, HashMap parameters, HashMap<String, String> getParameters)
  {
    String uri = parseUrl(url, parameters, getParameters);
//    PaasRestTemplate paasRestTemplate = (PaasRestTemplate)SpringContextUtil.getBean("externalRestTemplate");
//    ResponseEntity response = paasRestTemplate.getForEntity(uri, String.class, new Object[0]);
//    PaasBaseResponse paasBaseResponse = (PaasBaseResponse)JSON.parseObject((String)response.getBody(), new TypeReference()
//    {
//    }
//    , new Feature[0]);

//    return ((PaasBeanObject)paasBaseResponse.getBody()).getContent();
    return null;
  }
}